#include "BasicOccluder.h"
#include "Triangle.h"
#include "Intersect.h"
#include "HitState.h"

namespace baker
{
    //--------------------------------------------------------------------------
    BasicOccluder::BasicOccluder()
    {
    }

    //--------------------------------------------------------------------------
    BasicOccluder::~BasicOccluder()
    {
    }

    //--------------------------------------------------------------------------
    void BasicOccluder::addVertex( const Vec3 & vec3 )
    {
        mVerts.append( vec3 );
    }

    //--------------------------------------------------------------------------
    void BasicOccluder::addFace ( guint v0
                                , guint v1
                                , guint v2
                                )
    {
        mFaces.append( v0 );
        mFaces.append( v1 );
        mFaces.append( v2 );
    }

    //--------------------------------------------------------------------------
    void BasicOccluder::build()
    {
    }

    //--------------------------------------------------------------------------
    bool BasicOccluder::hitTestFast( const Ray3 & ray )
    {
        g_assert( ( mFaces.length() % 3 ) == 0 );

        Intersect<Ray3, Triangle> intersect;

        const guint len = mFaces.length();

        for ( guint i = 0; i< len; i += 3 )
        {
            Triangle tri( mVerts[ mFaces[ i + 0 ] ]
                        , mVerts[ mFaces[ i + 1 ] ]
                        , mVerts[ mFaces[ i + 2 ] ]
                        );

            intersect.test( ray, tri );

            if ( intersect.getHit() )
            {
                return true;
            }
        }

        return false;
    }

    //--------------------------------------------------------------------------
    void BasicOccluder::hitTestFull ( HitState & hitState
                                    , const Ray3 & ray
                                    )
    {
        g_assert( ( mFaces.length() % 3 ) == 0 );

        Intersect<Ray3, Triangle> intersect;

        const guint len = mFaces.length();

        for ( guint i = 0; i< len; i += 3 )
        {
            Triangle tri( mVerts[ mFaces[ i + 0 ] ]
                        , mVerts[ mFaces[ i + 1 ] ]
                        , mVerts[ mFaces[ i + 2 ] ]
                        );

            intersect.test( ray, tri );

            if ( intersect.getHit() )
            {
                HitInfo & hitInfo = hitState.appendHitInfo();

                hitInfo.dist = intersect.getRayHitDist();
                hitInfo.baryCoords = intersect.getBrayCoord();
                hitInfo.verts[0] = tri.v[0];
                hitInfo.verts[1] = tri.v[1];
                hitInfo.verts[2] = tri.v[2];
            }
        }
    }

    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
}
